﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using RenameSubtitles;

namespace RenameSubtitlesGUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           // Application.Run(new RenameSubtilesAboutBox());
            ChooseDirectory();
        }
        private static void ChooseDirectory()
        {
            string lastDir = Properties.Settings.Default.LastDirectory;
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            folderBrowserDialog1.SelectedPath = lastDir;
            folderBrowserDialog1.Description = "Choose a directory which contains episodes and subtitles";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = folderBrowserDialog1.SelectedPath;
                SaveSettings(path);
                bool rename = false;
                /*if (DialogResult.Yes == MessageBox.Show("Quieres renombrar los subtítulos (No para copiarlos)?", "Copy or Rename", MessageBoxButtons.YesNo))
                    rename = true;*/
                RenameSubtitles.RenameSubtitlesJob job = new RenameSubtitles.RenameSubtitlesJob(path, rename, true, new RenameSubtitlesJob.AskUserConfirmationDelegate(AskUserConfirmation));
                job.Start();

                Thread.SpinWait(10000);
                //string[] lines = File.ReadAllLines(path + "\\" + "RenameSubtitlesOutput.txt");
                /*
                FormResult frm = new FormResult();
                frm.lines = lines;
                frm.TopMost = true;
                if (DialogResult.Yes == frm.ShowDialog())
                {
                    job.DeleteSourceSubtitles();
                }
                */
            }
        }
        private static bool AskUserConfirmation(string folder, List<RenameSubtitles.RenameSubtitlesJob.Item> renameItems)
        {
            FormResult frm = new FormResult();
            frm.path = folder;
            frm.renameItems = renameItems;
            frm.TopMost = true;
            
            
            if (DialogResult.Yes == frm.ShowDialog())
            {
                return true;
            }
            else
                return false;
            

        }
        private static void SaveSettings(string path)
        {
            try
            {
                Properties.Settings.Default.LastDirectory = path;
                Properties.Settings.Default.Save();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


       
    }
}
